package com.lemon.cloud.redis.config;

import com.lemon.cloud.lock.DistributedLock;
import com.lemon.cloud.redis.lock.RedisDistributedLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Slf4j
@Configuration
@ConditionalOnClass({StringRedisTemplate.class})
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisDistributedLockAutoConfig {
    @Bean
    @ConditionalOnMissingBean
    public DistributedLock redisDistributedLock(StringRedisTemplate stringRedisTemplate) {
        RedisDistributedLock redisDistributedLock = new RedisDistributedLock();
        redisDistributedLock.setRedisTemplate(stringRedisTemplate);
        log.info("Create a distributed lock RedisDistributedLock");
        return redisDistributedLock;
    }
}